﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;

namespace Com.GUI.LibraryManager.Controls.SubControl
{
    /// <summary>
    /// Interaction logic for ucColdStorageProductDetail.xaml
    /// </summary>
    public partial class ucColdStorageProductDetail : UserControl
    {
        public ucColdStorageProductDetail()
        {
            InitializeComponent();
        }
        public void loadData()
        {
            cbxProduct.ItemsSource = null;
            cbxProduct.Items.Clear();
            cbxProduct.ItemsSource = LibraryManagerInCom.ProductList;
            cbxProduct.DisplayMemberPath = "product_name";
            cbxProduct.SelectedIndex = 0;
        }
        public void reLoad()
        {

            cbxProduct.ItemsSource = null;
            cbxProduct.Items.Clear();
            foreach (var item in LibraryManagerInCom.ProductList)
            {
                cbxProduct.Items.Add(item);
            }
            cbxProduct.DisplayMemberPath = "product_name";
            cbxProduct.SelectedIndex = 0;
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            loadData();
        }

        private void cbxProduct_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tb_product p = (tb_product)cbxProduct.SelectedItem;
            if (p != null)
            {
                if (p.product_type == 1)
                {
                    cbxProductType.SelectedIndex = 0;
                }
                else
                {
                    cbxProductType.SelectedIndex = 1;
                }
                txtSpecificHeat.Text = p.specific_heat.ToString();
            }

        }

        public void addNew()
        {
            cbxProduct.IsEditable = true;
            cbxProduct.Text = "";
            cbxProductType.SelectedItem = 0;
            txtSpecificHeat.Text = "";
        }
        public Boolean saveData()
        {
            Boolean success = false;
            tb_product p = new tb_product();
            p.product_name = cbxProduct.Text;
            if (cbxProductType.SelectedIndex == 0)
            {
                p.product_type = 1;
            }
            else
            {
                p.product_type = 2;
            }
            p.specific_heat = Convert.ToDouble(txtSpecificHeat.Text);
            if (p.product_name == "")
            {
                MessageBox.Show("All value must be fill!");
                success = false;
            }
            else
            {
                LibraryManagerInCom.addProduct(p);
                cbxProduct.IsEditable = false;
                reLoad();
                success = true;

            }

            return success;

        }
        public void delData()
        {
            tb_product p = (tb_product)cbxProduct.SelectedItem;
            LibraryManagerInCom.delProduct(p);
            reLoad();
        }

        public void copyData()
        {
            tb_product p = (tb_product)cbxProduct.SelectedItem;
            tb_product p1 = LibraryManagerInCom.copyProduct(p);
            cbxProduct.IsEditable = true;
            cbxProduct.Text = p1.product_name;
            if (p1.product_type == 1)
            {
                cbxProductType.SelectedIndex = 0;
            }
            else
            {
                cbxProductType.SelectedIndex = 1;
            }
            txtSpecificHeat.Text = p1.specific_heat.ToString();
        }

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion


    }
}
